<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2018/7/19
 * Time: 10:48
 */
namespace app\index\controller;


class Category extends Index
{
    protected $noNeedRight='*';
    protected $noNeedLogin='*';

    public function lists()
    {
        $cid=input('cid');
        $sort=input('sort');
        $cate=db('category')->where('id',$cid)->value('name');
        $child=db('category')->where('pid',$cid)->order('weigh asc')->select();
        $this->assign('child',$child);
        $this->assign('cid',$cid);
        $work=db('work')->where('cid','eq',$cid);
        if ($child){
            $work->whereOr('pid','in',array_column($child,'id'));
        }
        if ($sort){
            switch ($sort){
                case 'hot':$work->order(['view'=>'desc','id'=>'desc']);break;
                case 'new':$work->order(['ctime'=>'desc','id'=>'desc']);break;
                default:
                    $work->order('id desc');
            }
        }
        $data=$work->paginate(5);
        $this->assign('pid',input('pid',0));
        $this->assign('data',$data);

        $cate_part=part_words($cate);
        $cate_part=array_column($cate_part,'word');
        $link_word=db('work_words')->where('words','in',$cate_part)->distinct(true)->column('words');
        $link_word=array_unique(array_merge($cate_part,$link_word));
        $this->assign('link_word',$link_word);
        return view();
    }

    public function detail()
    {
        $id=input('id');
        $data=model('work')->find($id);
        $subscribe=db('work')->where('id','in',function ($query)use ($data,$id){
            $query->name('work_words')->where('words','in',$data['part_words'])->where('pid','neq',$id)->field('pid');
        })->limit(8)->select();
//        dump(db()->getLastSql());exit();
        db('work')->where('id',$id)->setInc('view');
        $this->assign('data',$data);
        $this->assign('subscribe',$subscribe);
        return view();
    }
}